<?php
/**
 * house controller
 */
namespace App\Http\Controllers;

use App\Service\HouseService;
use App\Validation\HouseListValidation;
use App\Providers\UrlServiceProvider;
use App\Service\ConfigsService;

use App\Service\HouseRecoService;
use App\Service\HouseSecoService;
use App\Service\HouseRentService;

use App\Service\MemberPraiseService;
use App\Service\MemberCollectService;

use App\Models\EwDics;

class HouseController extends BaseController
{
	const DISPLAY_TRUE = 1;		//显示标识：是
	const DISPLAY_FALSE = 0;	//显示标识：否
	const COLLECT_TRUE = 1;		//收藏标识：是
	const COLLECT_FALSE = 0;	//收藏标识：否
	const RECO_TRUE  = 1;		//获取首页列表标记
	const RECO_FALSE = 0;		//获取首页列表标记

	/**
	 * [indexAction 房源列表]
	 * @return [type] [description]
	 */
	public function indexAction()
	{	
		$citys = EwDics::find(['columns'=>'id,name', 'conditions'=>'pid = 1 and cate_id = -1']);
		$city_arr = [];
		foreach ($citys as $city) {
			$city_arr[$city->id] = $city->name;
		}

		$arr = new \stdClass;
		$arr->name = "house_tags";
		$tags_obj = ConfigsService::getVal($arr);

		$this->getUser(0);

		$raw = $this->validation(new HouseListValidation, $this->request->getJsonRawBody());
		$raw->city_id = $this->city_id;

		$raw->member_id = property_exists($raw,'member_id') ? $raw->member_id : 0;
		$raw->is_display = self::DISPLAY_TRUE;
		$data  = HouseService::getList($raw);

		// 获取点赞和收藏
		$praise_data = [];
		$collect_data = [];
		if ($raw->member_id && $raw->member_id >0 && count($data->toArray()) > 0){
			// $groupId = HouseService::getTableId();
			$groupId = 1;
			$id_arr = array_map('parent::getIdArray',$data->toArray());
			$praise_data  = MemberPraiseService::jugePraiseArr($raw->member_id, $groupId, $id_arr);
			$collect_data = MemberCollectService::jugeCollectArr($raw->member_id, $groupId, $id_arr);
		}

		$d = [];
		foreach ($data as $key => $value) {
			unset($value->index_by);
			unset($value->reco_by);
			$property = $value->property;

			if (preg_match('/商/', $property) && !preg_match('/住/', $property)){
				$value->house_tags = 'shangpu';
			}

			if (preg_match('/办/', $property) && !preg_match('/住/', $property)){
				$value->house_tags = 'xiezilou';
			}

			if (preg_match('/商/', $property)){
				$value->house_tags .= ',' . 'shangpu';
			}

			if (preg_match('/别墅/', $property)){
				$value->house_tags .= ',' . 'bieshu,dirongjilv,dimidu,cheweichongzu';
			}

			if (preg_match('/办/', $property)){
				$value->house_tags .= ',' . 'xiezilou';
			}

			$tag_arr = explode(",", $value->house_tags);
			$tag_arr = array_unique($tag_arr);
			if (count($tag_arr) > 3)
				$tag_arr = array_slice($tag_arr, 0, 3);

			$tag_ar = [];
			$house_tags = "";
			foreach ($tag_arr as $tag) {
				if (property_exists($tags_obj, $tag)){
					$tag_ar[] = $tags_obj->$tag;
					$house_tags .= $tags_obj->$tag->tag_name . ' ';
				}
			}

			$city = "";
			$city = in_array($value->city_id,array_keys($city_arr))  ? $city_arr[$value->city_id] : "";

			$d[$key] = $value;

			$d[$key]->city = $city;

			//判断缩略图是否有图且url是否完整
			$d[$key]->thumb = $value->thumb ? $this->getFullUrl($value->thumb, FALSE, $this->config->imgurl) : $this->config->imgurl . '/uploads/default.png';

			$d[$key]->price = $value->price ? $value->price . $value->unit : "价格待定";
			$d[$key]->area = intval($value->area) ? $value->area . '㎡' : "面积未知";
			$d[$key]->region = $value->region ? $value->region : "区域未知";
			$d[$key]->address = $value->address ? $value->address : "地址未知";

			$d[$key]->create_time = $this->getShowTime($value->create_time);
			
			$d[$key]->comment = $value->comment > 10000 ? floor($value->comment / 10000) . '万+' : $value->comment;
			$d[$key]->visit = $value->visit > 10000 ? floor($value->visit / 10000) . '万+' : $value->visit;
			$d[$key]->sources = $value->sources ? $value->sources : "觅窝";
			$d[$key]->house_tags = $value->house_tags ? $tag_ar : [];
			$d[$key]->layout = $value->layout ? $value->layout : "户型未知";

			$d[$key]->is_praise  = in_array($value->id, $praise_data) ? (string)self::COLLECT_TRUE : (string)self::COLLECT_FALSE;
			$d[$key]->is_collect = in_array($value->id, $collect_data) ? (string)self::COLLECT_TRUE : (string)self::COLLECT_FALSE;

			$is_praise = $d[$key]->is_praise ? 1 : 0;
			// $d[$key]->go_url = $this->config->gourl . $this->config->urls->house . $value->id . '/' . $value->groupId . '/' . $is_praise;
			$d[$key]->go_url = $this->config->gourl . $this->config->urls->house . $value->id;
			//第三方房源url
			$d[$key]->three_url = $value->three_url ? $value->three_url : $d[$key]->go_url;

			$value->build_type = $value->build_type  ? str_replace(',', ' ', $value->build_type) : '';
			$d[$key]->word = $city . ' ' . $house_tags . ' ' . $value->area . ' ' . $value->property . ' ' . $value->layout . ' ' . $value->build_type;
			// unset($value->unit);
			// unset($value->property);
			// unset($value->layout);
			unset($value->build_type);
		}
		// $count  = HouseService::getList($raw, TRUE);
		$count = HouseSecoService::count();
		$assign = [
			'total' => $count,
			'list'  => $d
		];

		return $this->echoJson($assign);
	}

	/**
	 * [detailAction 房源详情]
	 * @return [type] [description]
	 */
	public function detailAction()
	{
		$this->getUser(0);

		$id = $this->dispatcher->getParam('id');
		if (!$id)
			return $this->echoJson('not none');

		if (isset($this->user)){
			$data = HouseService::getInfo($id, $this->user->member_id);
		}else{
			$data = HouseService::getInfo($id);
		}

		$arr = new \stdClass();
		if ($data){
			$tag_obj = new \stdClass;
			$tag_obj->name = "house_tags";
			$tags_obj = ConfigsService::getVal($tag_obj);
			$tag_arr = explode(",", $data->house_tags);
			$tag_ar = [];
			foreach ($tag_arr as $tag) {
				if (property_exists($tags_obj, $tag))
					$tag_ar[] = $tags_obj->$tag;
			}
			foreach ($data as $key => $value) {
				$arr->$key = $value;
			}
			$arr->go_url = $this->config->gourl . $this->config->urls->house . $data->id;
			//判断缩略图是否有图且url是否完整
			$arr->thumb = $data->thumb ? $this->getFullUrl($data->thumb, FALSE, $this->config->imgurl) : $this->config->imgurl . '/uploads/default.png';
			if ($data->pics){
				$arr->pics = explode(',', $data->pics);
			}else{
				$arr->pics = [];
			}
			$arr->tags        = $data->tags ? $data->tags : "";
			$arr->house_tags  = $data->house_tags ? $tag_ar : [];
			$arr->house_holds = $data->house_holds . '户';
			$arr->plot        = $data->plot . '%';
			$arr->green       = $data->green . '%';
			$arr->top_floor   = $data->top_floor . '层';
			$arr->prop_fee    = $data->prop_fee . '元/平';
			$arr->prop_right  = $data->prop_right . '年';
			$arr->comment = $data->comment > 10000 ? floor($data->comment / 10000) . '万+' : $data->comment;
			$arr->visit = $data->visit > 10000 ? floor($data->visit / 10000) . '万+' : $data->visit;
			$arr->sources = $data->sources ? $data->sources : "觅窝";
		}

		return $this->echoJson($arr);
	}

	public function firstAction()
	{
		$arr = new \stdClass;
		$arr->name = "house_tags";
		$tags_obj = ConfigsService::getVal($arr);

		$this->getUser(0);
		$raw = $this->validation(new HouseListValidation, $this->request->getJsonRawBody());
		$raw->member_id = property_exists($raw,'member_id') ? $raw->member_id : 0;

		$raw->city_id = $this->city_id;
		$raw->is_reco = self::RECO_TRUE;
		$raw->is_display = self::DISPLAY_TRUE;

		$data = [];
		$data['new_total'] = HouseService::count();
		$data['seco_total'] = HouseSecoService::count();
		$data['rent_total'] = HouseRentService::count();
		$data['buy_total'] = $data['new_total'] + $data['seco_total'];

		if(isset($raw->type_id) && $raw->type_id!=''){
            $data=$this->getFirstPageRecHouseNew($raw, $tags_obj,$data);
        }else{//兼容之前的版本取数
            $data['tuijian']    = self::treco($raw, $tags_obj);
            $data['xinfang']  = self::tnewHouse($raw, $tags_obj);
            $data['ershoufang'] = self::tseco($raw, $tags_obj);
            $data['zufang'] = self::trent($raw, $tags_obj);
        }

        if ( array_key_exists("tuijian",$data) && Count($data['tuijian']) == 0){
			$raw->city_id = NULL;
			$data['tuijian']    = self::treco($raw, $tags_obj);
		}

		return $this->echoJson($data);
	}

	public function firsttestAction()
	{
		$arr = new \stdClass;
		$arr->name = "house_tags";
		$tags_obj = ConfigsService::getVal($arr);

		$this->getUser(0);
		$raw = $this->validation(new HouseListValidation, $this->request->getJsonRawBody());
		$raw->member_id = property_exists($raw,'member_id') ? $raw->member_id : 0;

		$raw->city_id = $this->city_id;
		$raw->is_reco = self::RECO_TRUE;
		$raw->is_display = self::DISPLAY_TRUE;

		$data = [];
		$data['new_total'] = intval(HouseService::count());
		$data['seco_total'] = intval(HouseSecoService::count());
		$data['rent_total'] = intval(HouseRentService::count());

		$data['buy_total'] = $data['new_total'] + $data['seco_total'];

		if(isset($raw->type_id) && $raw->type_id!=''){
            $data=$this->getFirstPageRecHouseNew($raw, $tags_obj,$data);
        }else{//兼容之前的版本取数

            $data['tuijian']    = self::treco($raw, $tags_obj);
            $data['xinfang']  = self::tnewHouse($raw, $tags_obj);
            $data['ershoufang'] = self::tseco($raw, $tags_obj);
            $data['zufang'] = self::trent($raw, $tags_obj);

        }
        if ( array_key_exists("tuijian",$data) && Count($data['tuijian']) == 0){
			$raw->city_id = NULL;
			$data['tuijian']    = self::treco($raw, $tags_obj);
		}

		return $this->echoJson($data);
	}

	/**
	 * [firstAction app首页组合数据  推荐房源+新房+二手房+租房]
	 * @return [type] [description]
	 */
	public function oldfirstAction()
	{
		$arr = new \stdClass;
		$arr->name = "house_tags";
		$tags_obj = ConfigsService::getVal($arr);

		$this->getUser(0);
		$raw = $this->validation(new HouseListValidation, $this->request->getJsonRawBody());
		$raw->member_id = property_exists($raw,'member_id') ? $raw->member_id : 0;

		$raw->city_id = $this->city_id;
		// $raw->city_id = NULL;
		$raw->is_reco = self::RECO_TRUE;
		$raw->is_display = self::DISPLAY_TRUE;

		$data = [];
		$data['new_total'] = HouseService::count();
		$data['seco_total'] = HouseSecoService::count();
		$data['rent_total'] = HouseRentService::count();
		$data['buy_total'] = $data['new_total'] + $data['seco_total'];

        if(isset($raw->type_id) && $raw->type_id!=''){
            $data=$this->getFirstPageRecHouse($raw, $tags_obj,$data);
        }else{//兼容之前的版本取数
            $data['tuijian']    = self::reco($raw, $tags_obj);
            $data['xinfang']  = self::newHouse($raw, $tags_obj);
            $data['ershoufang'] = self::seco($raw, $tags_obj);
            $data['zufang'] = self::rent($raw, $tags_obj);
        }
		if ( array_key_exists("tuijian",$data) && Count($data['tuijian']) == 0){
			$raw->city_id = NULL;
			$data['tuijian'] = self::reco($raw, $tags_obj);
		}
		//$profiles = $this->profiler->getProfiles();
		//foreach ($profiles as $profile) {
		//   $this->logger->log("SQL Statement: ".$profile->getSQLStatement(). PHP_EOL . "Total Elapsed Time: ". $profile->getTotalElapsedSeconds() .PHP_EOL);
		//}

		return $this->echoJson($data);
	}

	/**
	 * [getFirstPageRecHouseNew 改造 2017-09-14]
	 * @param  [type] $raw      [description]
	 * @param  [type] $tags_obj [description]
	 * @param  [type] $data     [description]
	 * @return [type]           [description]
	 */
	protected function getFirstPageRecHouseNew($raw, $tags_obj,$data){
        $type=intval($raw->type_id);
        if($type==1){
            $data['tuijian'] = self::treco($raw, $tags_obj);
        }elseif($type==2){
            $data['ershoufang'] = self::tseco($raw, $tags_obj);
        }elseif($type==3){
            $data['zufang'] = self::trent($raw, $tags_obj);
        }elseif($type==4){
            $data['xinfang']  = self::tnewHouse($raw, $tags_obj);
        }
        return $data;
    }

    protected function getFirstPageRecHouse($raw, $tags_obj,$data){
        $type=intval($raw->type_id);
        if($type==1){
            $data['tuijian'] = self::reco($raw, $tags_obj);
        }elseif($type==2){
            $data['ershoufang'] = self::seco($raw, $tags_obj);
        }elseif($type==3){
            $data['zufang'] = self::rent($raw, $tags_obj);
        }elseif($type==4){
            $data['xinfang']  = self::newHouse($raw, $tags_obj);
        }
        return $data;
    }

    protected function treco($raw, $tags_obj)
	{
		$data  = HouseRecoService::getHouseList($raw);
		$d = [];
		foreach ($data as $key => $value) {
			unset($value->index_by);
			unset($value->reco_by);
			$tag_arr = explode(",", $value->house_tags);
			if (count($tag_arr) > 3)
				$tag_arr = array_slice($tag_arr, 0, 3);
			$tag_ar = [];
			foreach ($tag_arr as $tag) {
				if (property_exists($tags_obj, $tag))
					$tag_ar[] = $tags_obj->$tag;
			}
			switch ($value->house_type) {
				case 'xinfang':
					$url = $this->config->urls->house;
					break;
				case 'ershoufang':
					$url = $this->config->urls->house_seco;
					break;
				case 'zufang':
					$url = $this->config->urls->house_rent;
					break;
				default:
					$url = $this->config->urls->house;
					break;
			}
			$d[$key] = $value;
			//判断缩略图是否有图且url是否完整
			$d[$key]->thumb = $value->thumb ? $this->getFullUrl($value->thumb, FALSE, $this->config->imgurl) : $this->config->imgurl . '/uploads/default.png';

			$d[$key]->area = intval($value->area) ? $value->area . '㎡' : "面积未知";
			$d[$key]->region = $value->region ? $value->region : "区域未知";
			$d[$key]->address = $value->address ? $value->address : "地址未知";
			$d[$key]->price = $value->price ? $value->price . $value->unit : "价格待定";
			unset($d[$key]->unit);
			$d[$key]->total_price = $value->total_price ? $value->total_price . "万/套" : "总价未知";
			$d[$key]->house_orien = $value->house_orien ? $value->house_orien : "南";

			$is_praise = $value->is_praise ? 1 : 0;
			// $d[$key]->go_url = $this->config->gourl . $url . $value->id . '/' . $value->groupId . '/' . $is_praise;
			$d[$key]->go_url = $this->config->gourl . $url . $value->id;
			//第三方房源url
			$d[$key]->three_url = $value->three_url ? $value->three_url : $d[$key]->go_url;
			$d[$key]->is_collect = intval($value->is_collect) ? (string)self::COLLECT_TRUE : (string)self::COLLECT_FALSE;
			$d[$key]->is_praise =  intval($value->is_praise) ? (string)self::COLLECT_TRUE : (string)self::COLLECT_FALSE;
			// $d[$key]->comment = $value->comment > 10000 ? floor($value->comment / 10000) . '万+' : $value->comment;
			// $d[$key]->visit = $value->visit > 10000 ? floor($value->visit / 10000) . '万+' : $value->visit;
			$d[$key]->sources = $value->sources ? $value->sources : "觅窝";

			$d[$key]->create_time = $this->getShowTime($value->create_time);

			$d[$key]->house_tags = $value->house_tags ? $tag_ar : [];
			$d[$key]->layout = $value->layout ? $value->layout : "户型暂无";
		}
		return $d;
	}

	protected function tnewHouse($raw, $tags_obj)
	{
		$data  = HouseService::getHouseList($raw);

		//获取点赞和收藏
		$praise_data = [];
		$collect_data = [];
		if (isset($raw->member_id) && $raw->member_id >0 && count($data) > 0){
			// $groupId = HouseService::getTableId();
			$groupId = 1;
			$id_arr = array_map('parent::getIdArray',$data);
			$praise_data  = MemberPraiseService::jugePraiseArr($raw->member_id, $groupId, $id_arr);
			$collect_data = MemberCollectService::jugeCollectArr($raw->member_id, $groupId, $id_arr);
		}

		$d = [];
		foreach ($data as $key => $value) {
			unset($value->index_by);
			unset($value->reco_by);
			$tag_arr = explode(",", $value->house_tags);
			if (count($tag_arr) > 3)
				$tag_arr = array_slice($tag_arr, 0, 3);
			$tag_ar = [];
			foreach ($tag_arr as $tag) {
				if (property_exists($tags_obj, $tag))
					$tag_ar[] = $tags_obj->$tag;
			}

			$d[$key] = $value;
			//判断缩略图是否有图且url是否完整
			$d[$key]->thumb = $value->thumb ? $this->getFullUrl($value->thumb, FALSE, $this->config->imgurl) : $this->config->imgurl . '/uploads/default.png';

			$d[$key]->area = intval($value->area) ? $value->area . '㎡' : "面积未知";
			$d[$key]->region = $value->region ? $value->region : "区域未知";
			$d[$key]->address = $value->address ? $value->address : "地址未知";
			$d[$key]->price = $value->price ? $value->price . $value->unit : "价格待定";

			$d[$key]->comment = $value->comment > 10000 ? floor($value->comment / 10000) . '万+' : $value->comment;
			$d[$key]->visit = $value->visit > 10000 ? floor($value->visit / 10000) . '万+' : $value->visit;
			$d[$key]->sources = $value->sources ? $value->sources : "觅窝";

			$d[$key]->create_time = $this->getShowTime($value->create_time);

			$d[$key]->house_tags = $value->house_tags ? $tag_ar : [];
			unset($d[$key]->unit);
			$d[$key]->layout = $value->layout ? $value->layout : "户型暂无";

			$d[$key]->is_praise  = in_array($value->id, $praise_data) ? (string)self::COLLECT_TRUE : (string)self::COLLECT_FALSE;
			$d[$key]->is_collect = in_array($value->id, $collect_data) ? (string)self::COLLECT_TRUE : (string)self::COLLECT_FALSE;

			$is_praise = $d[$key]->is_praise ? 1 : 0;
			// $d[$key]->go_url = $this->config->gourl . $this->config->urls->house . $value->id . '/' . $value->groupId . '/' . $is_praise;
			$d[$key]->go_url = $this->config->gourl . $this->config->urls->house . $value->id;
			//第三方房源url
			$d[$key]->three_url = $value->three_url ? $value->three_url : $d[$key]->go_url;
		}
		return $d;
	}

	public function tseco($raw, $tags_obj)
	{
		$data  = HouseSecoService::getHouseList($raw);

		//获取点赞和收藏
		$praise_data = [];
		$collect_data = [];
		if (isset($raw->member_id) && $raw->member_id >0 && count($data) > 0){
			// $groupId = HouseSecoService::getTableId();
			$groupId = 8;
			$id_arr = array_map('parent::getIdArray',$data);
			$praise_data  = MemberPraiseService::jugePraiseArr($raw->member_id, $groupId, $id_arr);
			$collect_data = MemberCollectService::jugeCollectArr($raw->member_id, $groupId, $id_arr);
		}

		$d = [];
		foreach ($data as $key => $value) {
			unset($value->index_by);
			unset($value->reco_by);
			$tag_arr = explode(",", $value->house_tags);
			if (count($tag_arr) > 3)
				$tag_arr = array_slice($tag_arr, 0, 3);
			$tag_ar = [];
			foreach ($tag_arr as $tag) {
				if (property_exists($tags_obj, $tag))
					$tag_ar[] = $tags_obj->$tag;
			}

			$d[$key] = $value;
			$d[$key]->house_tags = $value->house_tags ? $tag_ar : [];

			//判断缩略图是否有图且url是否完整
			$d[$key]->thumb = $value->thumb ? $this->getFullUrl($value->thumb, FALSE, $this->config->imgurl) : $this->config->imgurl . '/uploads/default.png';

			$d[$key]->create_time = $this->getShowTime($value->create_time);
			$d[$key]->region = $value->region ? $value->region : "区域未知";

			$d[$key]->sources = $value->sources ? $value->sources : "觅窝";
			
			$d[$key]->layout = $value->layout ? $value->layout : "户型暂无";
			$d[$key]->area = intval($value->area) ? $value->area . '㎡' : "面积未知";
			$d[$key]->house_orien = $value->house_orien ? $value->house_orien : "朝向不明";

			$d[$key]->price = $value->price ? $value->price . "元/㎡" : "单价未知";
			$d[$key]->total_price = $value->total_price ? $value->total_price . "万" : "总价未知";
			$d[$key]->comment = $value->comment > 10000 ? floor($value->comment / 10000) . '万+' : $value->comment;
			$d[$key]->visit = $value->visit > 10000 ? floor($value->visit / 10000) . '万+' : $value->visit;		

			$d[$key]->is_praise  = in_array($value->id, $praise_data) ? (string)self::COLLECT_TRUE : (string)self::COLLECT_FALSE;
			$d[$key]->is_collect = in_array($value->id, $collect_data) ? (string)self::COLLECT_TRUE : (string)self::COLLECT_FALSE;

			$is_praise = $d[$key]->is_praise ? 1 : 0;
			// $d[$key]->go_url = $this->config->gourl . $this->config->urls->house_seco . $value->id . '/' . $value->groupId . '/' . $is_praise;
			$d[$key]->go_url = $this->config->gourl . $this->config->urls->house_seco . $value->id;
			//第三方房源url
			$d[$key]->three_url = $value->three_url ? $value->three_url : $d[$key]->go_url;
		}

		return $d;
	}

	/**
	 * [rent 租房数据预处理]
	 * @param  [type] $data [description]
	 * @return [type]       [description]
	 */
	protected function trent($raw, $tags_obj)
	{
		$data  = HouseRentService::getHouseList($raw);

		//获取点赞和收藏
		$praise_data = [];
		$collect_data = [];
		if (isset($raw->member_id) && $raw->member_id >0 && count($data) > 0){
			// $groupId = HouseRentService::getTableId();
			$groupId = 9;
			$id_arr = array_map('parent::getIdArray',$data);
			$praise_data  = MemberPraiseService::jugePraiseArr($raw->member_id, $groupId, $id_arr);
			$collect_data = MemberCollectService::jugeCollectArr($raw->member_id, $groupId, $id_arr);
		}
		
		$d = [];
		foreach ($data as $key => $value) {
			unset($value->index_by);
			unset($value->reco_by);
			$tag_arr = explode(",", $value->house_tags);
			if (count($tag_arr) > 3)
				$tag_arr = array_slice($tag_arr, 0, 3);
			$tag_ar = [];
			foreach ($tag_arr as $tag) {
				if (property_exists($tags_obj, $tag))
					$tag_ar[] = $tags_obj->$tag;
			}

			$d[$key] = $value;
			$d[$key]->house_tags = $value->house_tags ? $tag_ar : [];

			//判断缩略图是否有图且url是否完整
			$d[$key]->thumb = $value->thumb ? $this->getFullUrl($value->thumb, FALSE, $this->config->imgurl) : $this->config->imgurl . '/uploads/default.png';

			$d[$key]->create_time = $this->getShowTime($value->create_time);

			$d[$key]->region = $value->region ? $value->region : "区域未知";

			$d[$key]->sources = $value->sources ? $value->sources : "觅窝";
			
			$d[$key]->layout = $value->layout ? $value->layout : "户型暂无";
			$d[$key]->area = intval($value->area) ? $value->area . '㎡' : "面积未知";
			$d[$key]->house_orien = $value->house_orien ? $value->house_orien : "南";

			$d[$key]->price = $value->price ? $value->price . "元/月" : "面议";
			$d[$key]->comment = $value->comment > 10000 ? floor($value->comment / 10000) . '万+' : $value->comment;
			$d[$key]->visit = $value->visit > 10000 ? floor($value->visit / 10000) . '万+' : $value->visit;

			$d[$key]->is_praise  = in_array($value->id, $praise_data) ? (string)self::COLLECT_TRUE : (string)self::COLLECT_FALSE;
			$d[$key]->is_collect = in_array($value->id, $collect_data) ? (string)self::COLLECT_TRUE : (string)self::COLLECT_FALSE;

			$is_praise = $d[$key]->is_praise ? 1 : 0;
			// $d[$key]->go_url = $this->config->gourl . $this->config->urls->house_rent . $value->id . '/' . $value->groupId . '/' . $is_praise;
			$d[$key]->go_url = $this->config->gourl . $this->config->urls->house_rent . $value->id;
			//第三方房源url
			$d[$key]->three_url = $value->three_url ? $value->three_url : $d[$key]->go_url;
		}
		return $d;
	}

	protected function reco($raw, $tags_obj)
	{
		$data  = HouseRecoService::getList($raw);
		$d = [];
		foreach ($data as $key => $value) {
			unset($value->index_by);
			unset($value->reco_by);
			$tag_arr = explode(",", $value->house_tags);
			if (count($tag_arr) > 3)
				$tag_arr = array_slice($tag_arr, 0, 3);
			$tag_ar = [];
			foreach ($tag_arr as $tag) {
				if (property_exists($tags_obj, $tag))
					$tag_ar[] = $tags_obj->$tag;
			}
			switch ($value->house_type) {
				case 'xinfang':
					$url = $this->config->urls->house;
					break;
				case 'ershoufang':
					$url = $this->config->urls->house_seco;
					break;
				case 'zufang':
					$url = $this->config->urls->house_rent;
					break;
				default:
					$url = $this->config->urls->house;
					break;
			}
			$d[$key] = $value;
			//判断缩略图是否有图且url是否完整
			$d[$key]->thumb = $value->thumb ? $this->getFullUrl($value->thumb, FALSE, $this->config->imgurl) : $this->config->imgurl . '/uploads/default.png';

			$d[$key]->area = intval($value->area) ? $value->area . '㎡' : "面积未知";
			$d[$key]->region = $value->region ? $value->region : "区域未知";
			$d[$key]->address = $value->address ? $value->address : "地址未知";
			$d[$key]->price = $value->price ? $value->price . $value->unit : "价格待定";
			unset($d[$key]->unit);
			$d[$key]->total_price = $value->total_price ? $value->total_price . "万/套" : "总价未知";
			$d[$key]->house_orien = $value->house_orien ? $value->house_orien : "南";

			$is_praise = $value->is_praise ? 1 : 0;
			// $d[$key]->go_url = $this->config->gourl . $url . $value->id . '/' . $value->groupId . '/' . $is_praise;
			$d[$key]->go_url = $this->config->gourl . $url . $value->id;
			//第三方房源url
			$d[$key]->three_url = $value->three_url ? $value->three_url : $d[$key]->go_url;
			$d[$key]->is_collect = intval($value->is_collect) ? (string)self::COLLECT_TRUE : (string)self::COLLECT_FALSE;
			$d[$key]->is_praise =  intval($value->is_praise) ? (string)self::COLLECT_TRUE : (string)self::COLLECT_FALSE;
			// $d[$key]->comment = $value->comment > 10000 ? floor($value->comment / 10000) . '万+' : $value->comment;
			// $d[$key]->visit = $value->visit > 10000 ? floor($value->visit / 10000) . '万+' : $value->visit;
			$d[$key]->sources = $value->sources ? $value->sources : "觅窝";

			$d[$key]->create_time = $this->getShowTime($value->create_time);

			$d[$key]->house_tags = $value->house_tags ? $tag_ar : [];
			$d[$key]->layout = $value->layout ? $value->layout : "户型暂无";
		}
		return $d;
	}

	protected function newHouse($raw, $tags_obj)
	{
		$data  = HouseService::getList($raw);

		//获取点赞和收藏
		$praise_data = [];
		$collect_data = [];
		if (isset($raw->member_id) && $raw->member_id >0 && count($data->toArray()) > 0){
			// $groupId = HouseService::getTableId();
			$groupId = 1;
			$id_arr = array_map('parent::getIdArray',$data->toArray());
			$praise_data  = MemberPraiseService::jugePraiseArr($raw->member_id, $groupId, $id_arr);
			$collect_data = MemberCollectService::jugeCollectArr($raw->member_id, $groupId, $id_arr);
		}

		$d = [];
		foreach ($data as $key => $value) {
			unset($value->index_by);
			unset($value->reco_by);
			$tag_arr = explode(",", $value->house_tags);
			if (count($tag_arr) > 3)
				$tag_arr = array_slice($tag_arr, 0, 3);
			$tag_ar = [];
			foreach ($tag_arr as $tag) {
				if (property_exists($tags_obj, $tag))
					$tag_ar[] = $tags_obj->$tag;
			}

			$d[$key] = $value;
			//判断缩略图是否有图且url是否完整
			$d[$key]->thumb = $value->thumb ? $this->getFullUrl($value->thumb, FALSE, $this->config->imgurl) : $this->config->imgurl . '/uploads/default.png';

			$d[$key]->area = intval($value->area) ? $value->area . '㎡' : "面积未知";
			$d[$key]->region = $value->region ? $value->region : "区域未知";
			$d[$key]->address = $value->address ? $value->address : "地址未知";
			$d[$key]->price = $value->price ? $value->price . $value->unit : "价格待定";

			$d[$key]->comment = $value->comment > 10000 ? floor($value->comment / 10000) . '万+' : $value->comment;
			$d[$key]->visit = $value->visit > 10000 ? floor($value->visit / 10000) . '万+' : $value->visit;
			$d[$key]->sources = $value->sources ? $value->sources : "觅窝";

			$d[$key]->create_time = $this->getShowTime($value->create_time);

			$d[$key]->house_tags = $value->house_tags ? $tag_ar : [];
			unset($d[$key]->unit);
			$d[$key]->layout = $value->layout ? $value->layout : "户型暂无";

			$d[$key]->is_praise  = in_array($value->id, $praise_data) ? (string)self::COLLECT_TRUE : (string)self::COLLECT_FALSE;
			$d[$key]->is_collect = in_array($value->id, $collect_data) ? (string)self::COLLECT_TRUE : (string)self::COLLECT_FALSE;

			$is_praise = $d[$key]->is_praise ? 1 : 0;
			// $d[$key]->go_url = $this->config->gourl . $this->config->urls->house . $value->id . '/' . $value->groupId . '/' . $is_praise;
			$d[$key]->go_url = $this->config->gourl . $this->config->urls->house . $value->id;
			//第三方房源url
			$d[$key]->three_url = $value->three_url ? $value->three_url : $d[$key]->go_url;
		}
		return $d;
	}

	public function seco($raw, $tags_obj)
	{
		$data  = HouseSecoService::getList($raw);

		//获取点赞和收藏
		$praise_data = [];
		$collect_data = [];
		if (isset($raw->member_id) && $raw->member_id >0 && count($data->toArray()) > 0){
			// $groupId = HouseSecoService::getTableId();
			$groupId = 8;
			$id_arr = array_map('parent::getIdArray',$data->toArray());
			$praise_data  = MemberPraiseService::jugePraiseArr($raw->member_id, $groupId, $id_arr);
			$collect_data = MemberCollectService::jugeCollectArr($raw->member_id, $groupId, $id_arr);
		}

		$d = [];
		foreach ($data as $key => $value) {
			unset($value->index_by);
			unset($value->reco_by);
			$tag_arr = explode(",", $value->house_tags);
			if (count($tag_arr) > 3)
				$tag_arr = array_slice($tag_arr, 0, 3);
			$tag_ar = [];
			foreach ($tag_arr as $tag) {
				if (property_exists($tags_obj, $tag))
					$tag_ar[] = $tags_obj->$tag;
			}

			$d[$key] = $value;
			$d[$key]->house_tags = $value->house_tags ? $tag_ar : [];

			//判断缩略图是否有图且url是否完整
			$d[$key]->thumb = $value->thumb ? $this->getFullUrl($value->thumb, FALSE, $this->config->imgurl) : $this->config->imgurl . '/uploads/default.png';

			$d[$key]->create_time = $this->getShowTime($value->create_time);
			$d[$key]->region = $value->region ? $value->region : "区域未知";

			$d[$key]->sources = $value->sources ? $value->sources : "觅窝";
			
			$d[$key]->layout = $value->layout ? $value->layout : "户型暂无";
			$d[$key]->area = intval($value->area) ? $value->area . '㎡' : "面积未知";
			$d[$key]->house_orien = $value->house_orien ? $value->house_orien : "朝向不明";

			$d[$key]->price = $value->price ? $value->price . "元/㎡" : "单价未知";
			$d[$key]->total_price = $value->total_price ? $value->total_price . "万" : "总价未知";
			$d[$key]->comment = $value->comment > 10000 ? floor($value->comment / 10000) . '万+' : $value->comment;
			$d[$key]->visit = $value->visit > 10000 ? floor($value->visit / 10000) . '万+' : $value->visit;		

			$d[$key]->is_praise  = in_array($value->id, $praise_data) ? (string)self::COLLECT_TRUE : (string)self::COLLECT_FALSE;
			$d[$key]->is_collect = in_array($value->id, $collect_data) ? (string)self::COLLECT_TRUE : (string)self::COLLECT_FALSE;

			$is_praise = $d[$key]->is_praise ? 1 : 0;
			// $d[$key]->go_url = $this->config->gourl . $this->config->urls->house_seco . $value->id . '/' . $value->groupId . '/' . $is_praise;
			$d[$key]->go_url = $this->config->gourl . $this->config->urls->house_seco . $value->id;
			//第三方房源url
			$d[$key]->three_url = $value->three_url ? $value->three_url : $d[$key]->go_url;
		}

		return $d;
	}

	/**
	 * [rent 租房数据预处理]
	 * @param  [type] $data [description]
	 * @return [type]       [description]
	 */
	protected function rent($raw, $tags_obj)
	{
		$data  = HouseRentService::getList($raw);

		//获取点赞和收藏
		$praise_data = [];
		$collect_data = [];
		if (isset($raw->member_id) && $raw->member_id >0 && count($data->toArray()) > 0){
			// $groupId = HouseRentService::getTableId();
			$groupId = 9;
			$id_arr = array_map('parent::getIdArray',$data->toArray());
			$praise_data  = MemberPraiseService::jugePraiseArr($raw->member_id, $groupId, $id_arr);
			$collect_data = MemberCollectService::jugeCollectArr($raw->member_id, $groupId, $id_arr);
		}
		
		$d = [];
		foreach ($data as $key => $value) {
			unset($value->index_by);
			unset($value->reco_by);
			$tag_arr = explode(",", $value->house_tags);
			if (count($tag_arr) > 3)
				$tag_arr = array_slice($tag_arr, 0, 3);
			$tag_ar = [];
			foreach ($tag_arr as $tag) {
				if (property_exists($tags_obj, $tag))
					$tag_ar[] = $tags_obj->$tag;
			}

			$d[$key] = $value;
			$d[$key]->house_tags = $value->house_tags ? $tag_ar : [];

			//判断缩略图是否有图且url是否完整
			$d[$key]->thumb = $value->thumb ? $this->getFullUrl($value->thumb, FALSE, $this->config->imgurl) : $this->config->imgurl . '/uploads/default.png';

			$d[$key]->create_time = $this->getShowTime($value->create_time);

			$d[$key]->region = $value->region ? $value->region : "区域未知";

			$d[$key]->sources = $value->sources ? $value->sources : "觅窝";
			
			$d[$key]->layout = $value->layout ? $value->layout : "户型暂无";
			$d[$key]->area = intval($value->area) ? $value->area . '㎡' : "面积未知";
			$d[$key]->house_orien = $value->house_orien ? $value->house_orien : "南";

			$d[$key]->price = $value->price ? $value->price . "元/月" : "面议";
			$d[$key]->comment = $value->comment > 10000 ? floor($value->comment / 10000) . '万+' : $value->comment;
			$d[$key]->visit = $value->visit > 10000 ? floor($value->visit / 10000) . '万+' : $value->visit;

			$d[$key]->is_praise  = in_array($value->id, $praise_data) ? (string)self::COLLECT_TRUE : (string)self::COLLECT_FALSE;
			$d[$key]->is_collect = in_array($value->id, $collect_data) ? (string)self::COLLECT_TRUE : (string)self::COLLECT_FALSE;

			$is_praise = $d[$key]->is_praise ? 1 : 0;
			// $d[$key]->go_url = $this->config->gourl . $this->config->urls->house_rent . $value->id . '/' . $value->groupId . '/' . $is_praise;
			$d[$key]->go_url = $this->config->gourl . $this->config->urls->house_rent . $value->id;
			//第三方房源url
			$d[$key]->three_url = $value->three_url ? $value->three_url : $d[$key]->go_url;
		}
		return $d;
	}

	public function logAction()
	{
		$area = '温江区';
		$area = preg_match('/(区|县|市)$/', $area) ? mb_substr($area, 0, -1,"UTF-8") : $area;
		$citys = EwDics::find(['columns'=>'id,name,pid,lon,lat', 'conditions'=>'pid = 63 AND cate_id = -1'])->toArray();
		foreach ($citys as $key => $city) {
			if (preg_match('/'.$city['name'].'/',$area)){
				var_dump($city);
			}
		}
		die;
		$citys = EwDics::find(['columns'=>'id,name,pid,pid as city', 'conditions'=>'cate_id = -1'])->toArray();
		$city_arr = [];
		foreach ($citys as $key => $value) {
			$city_arr[$value['id']] = $value;
		}
		$data = [];
		foreach ($citys as $key => $value) {
			if ($value['city'] > 1){
				$value['city'] = $city_arr[$value['city']]['name'];
			}else{
				$value['city'] = $value['name'];
			}
			$data[$key] = $value;
		}

		foreach ($data as $key => $value) {
			$name = $value['name'];
			$city = $value['city'];
			$ak   = "lQGoEIx7wzGrUHyMbgufPhEp00GGcDmR";
			$output = "json";
			// $lon = '104.097564';
			// $lat = '30.679083';
			$url = "http://api.map.baidu.com/geocoder/v2/?output={$output}&address={$name}&city={$city}&ak={$ak}";
			// $url = "http://api.map.baidu.com/geocoder/v2/?output={$output}&address={$name}&ak={$ak}";
			// $url = "http://api.map.baidu.com/geocoder/v2/?location=39.983424,116.322987&output=json&pois=1&ak=lQGoEIx7wzGrUHyMbgufPhEp00GGcDmR";
			$json = file_get_contents($url); 
			$data[$key]['lon'] = 0;
			$data[$key]['lat'] = 0;
			$obj = json_decode($json);
			if ($obj->status == 0){
				$lon = $obj->result->location->lng;
				$lat = $obj->result->location->lat;
				$data[$key]['lon'] = $lon;
				$data[$key]['lat'] = $lat;
			}	
		}
		var_dump($data);die;
		foreach ($data as $key => $value) {
			$raw = EwDics::findFirst($value['id']);
			$raw->lon = $value['lon'];
			$raw->lat = $value['lat'];
			$raw->save();
		}
// 		$name = "成都";
// 		$city = "成都";
// 		$ak   = "lQGoEIx7wzGrUHyMbgufPhEp00GGcDmR";
// 		$output = "json";
// 		// $lon = '104.097564';
// 		// $lat = '30.679083';
// 		$url = "http://api.map.baidu.com/geocoder/v2/?output={$output}&address={$name}&city={$city}&ak={$ak}";
// 		// $url = "http://api.map.baidu.com/geocoder/v2/?output={$output}&address={$name}&ak={$ak}";
// 		// $url = "http://api.map.baidu.com/geocoder/v2/?location=39.983424,116.322987&output=json&pois=1&ak=lQGoEIx7wzGrUHyMbgufPhEp00GGcDmR";
// 		$json = file_get_contents($url); 
// // return $json;
// 		$obj = json_decode($json);

// 		if ($obj->status == 0){
// 			$lon = $obj->result->location->lng;
// 			$lat = $obj->result->location->lat;
// 		}
		// $url = "http://api.map.baidu.com/geocoder/v2/?location={$lat},{$lon}&output=json&pois=1&ak=lQGoEIx7wzGrUHyMbgufPhEp00GGcDmR";
		// $json = file_get_contents($url);
		// return $json; 
		// return $data->result->location;
		// $ch = curl_int();
		// curl_setopt($ch, CURLOPT_URL, $url);
		// curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		// curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
		// $r = curl_exec($ch);
		// curl_close($ch);
		// return $r;
	}

	/*public function testAction()
	{
		$data = [
			"putongzhuzhai" => ["name"=>"普通住宅","color"=>"#FF0000"],
			"dimidu"=>["name"=>"低密度","color"=>"#00FF00"],
			"cheweichongzu"=>["name"=>"车位充足","color"=>"#0000FF"],
			"bieshu"=>["name"=>"别墅","color"=>"#FFCC00"],
		];
		$arr = new \stdClass;
		$arr->name = "house_tags";
		$data = ConfigsService::getVal($arr);
		$arr->tags = "dimidu,bieshu";
		$tags = explode(",", $arr->tags);
		foreach ($tags as $key => $value) {
			$ar[] = $data->$value;
		}
		$arr->tags = $ar;

		$arr = [
			1  => "ew_house",
			2  => "ew_news",
			3  => "ew_deco",
			4  => "ew_data",
			5  => "ew_comment",
			6  => "ew_video",
			7  => "ew_deco_comp",
			8  => "ew_house_seco",
			9  => "ew_house_rent",
			10 => "ew_house_reco",
			11 => "ew_news_orig",
			12 => "ew_ads",
			13 => "ew_ads_pos",
			14 => "ew_bill",
			15 => "ew_cats",
			16 => "ew_city",
			17 => "ew_configs",
			18 => "ew_dics",
			19 => "ew_feedbacks",
			20 => "ew_gain",
			21 => "ew_live_recorde",
			22 => "ew_member",
			23 => "ew_member_collect",
			24 => "ew_member_praise",
			25 => "ew_member_subs",
			26 => "ew_page",
			27 => "ew_push",
			28 => "ew_roles",
			29 => "ew_log_sys",
			30 => "ew_login3",
			31 => "ew_member_auth",
			32 => "ew_member_log",
			33 => "ew_nodes",
			34 => "ew_nodes_auth",
			35 => "ew_sms",
			36 => "ew_uploads",
			37 => "ew_user",
			38 => "ew_reco",
		];
		$arr = array_filter($arr);
		return $this->echoJson($arr);
	}*/

	public function redisAction()
	{ 
		//设置twjteststring  
		$this->redis->setex('twjteststring','300','test string title content');  
		$twjteststring = $this->redis->get('twjteststring');  
		echo 'string<br/>';  
		print_r($twjteststring);  
		echo "<hr/><br/>";  
		//设置hash  
		echo 'hash<br/>';
	}
}